/*
 * Package il.ac.biu.cs.grossmm.impl.server
 * File Registry.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.server;

import il.ac.biu.cs.grossmm.api.server.Loader;
import il.ac.biu.cs.grossmm.api.server.LoaderSupport;
import il.ac.biu.cs.grossmm.api.server.Service;
import il.ac.biu.cs.grossmm.api.server.ServiceListener;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.server.Wrapped;
import il.ac.biu.cs.grossmm.api.server.Wrapper;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Registry extends LoaderSupport implements Loader, ServiceListener {
	static class Singleton {
		Class clazz;

		Object object;

		public Singleton(Class objectClass)
		{
			this.clazz = objectClass;
		}

		synchronized Object getInstance() throws InstantiationException,
			IllegalAccessException
		{
			if (object == null) {
				object = clazz.newInstance();
			}

			return object;
		}
	}

	final Map<Object, Object> map = new HashMap<Object, Object>();
	final Map<Object, Set<Object>> hintsMap = new HashMap<Object, Set<Object>>();
	final Map<Object, Set<Object>> depsMap = new HashMap<Object, Set<Object>>(); 

	final Loader fallbackLoader;

	public Registry()
	{
		this(null);
	}

	public Registry(Loader fallbackLoader)
	{
		this.fallbackLoader = fallbackLoader;
	}

	protected Wrapped doLoad(Wrapper wrapper, Object hint) throws Exception
	{
		Object o;
		Wrapped w;

		o = map.get(hint);
		
		if( o instanceof Singleton )
			o = ((Singleton)o).getInstance();

		if (o == null)
			if (fallbackLoader == null)
				return null;
			else {
				w = fallbackLoader.load(wrapper, hint);
				o = w.getObject();
			}
		else {
			w = wrapper.wrap(o);
			w.setLoader(this);
		}
		
		Set<Object> hints = hintsMap.get(o);
		
		if( hints == null ) {
			hints = new HashSet<Object>();
			hintsMap.put(o, hints);
		}
		
		hints.add(hint);
		
		return w;
	}

	public void register(Object hint, Object object)
	{
		map.put(hint, object);
	}
	
	public void registerClass(String hint, Class objectClass)
	{
		Singleton s = new Singleton(objectClass);
		map.put(hint, s);
	}
	
	public <T> void registerClass(Class<T> clazz)
	{
		registerClass(clazz, clazz);
	}

	public <I, C extends I> void registerClass(Class<I> hint,
		Class<C> objectClass)
	{
		Singleton s = new Singleton(objectClass);
		map.put(hint, s);
	}

	public void deregister(Object hint)
	{
		map.remove(hint);
	}
	
	public void addDependency(Object depending, Object dependant) {
		Set<Object> set = depsMap.get(depending);
		
		if( set == null ) {
			set = new HashSet<Object>();
			depsMap.put(depending, set);
		}
		
		set.add(dependant);
	}

	public void beforeStart(ServiceManager sm, Service service) throws Exception
	{
		Set<Object> hints = hintsMap.get(service);
		
		assert hints != null; // we loaded it
		
		for( Object hint : hints ) {
			Set<Object> deps = depsMap.get(hint);
			if( deps == null )
				continue;
			
			for( Object dep : deps)
				sm.requireService(dep);
		}
	}

	
	public void afterStart(ServiceManager sm, Service service)
	{
	}

	public void beforeStop(ServiceManager sm, Service service)
	{
		
	}
	
	public void afterStop(ServiceManager sm, Service service)
	{
		Set<Object> hints = hintsMap.get(service);
		
		assert hints != null; // we loaded it
		
		for( Object hint : hints ) {
			Set<Object> deps = depsMap.get(hint);
			if( deps == null )
				continue;
			
			for( Object dep : deps)
				sm.releaseService(dep);
		}
	}

}
